package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.cache.CourseCache;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.PreCourse;
import com.example.teesystem.entity.vo.preCourse.PreCourseGet;
import com.example.teesystem.entity.vo.preCourse.PreCourseInsert;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.PreCourseService;
import com.example.teesystem.mapper.PreCourseMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author 12510
* @description 针对表【t_pre_course】的数据库操作Service实现
* @createDate 2022-05-27 11:14:08
*/
@Service
@AllArgsConstructor
public class PreCourseServiceImpl extends ServiceImpl<PreCourseMapper, PreCourse>
    implements PreCourseService{

    private final PreCourseMapper preCourseMapper;

    @Override
    @Transactional
    public Response insertPreCourse(PreCourseInsert preCourseInsert) {
        try{
            for (int i = 0; i < preCourseInsert.getCourseName().length; i++) {
                PreCourse preCourse = new PreCourse();
                preCourse.setCourseId(preCourseInsert.getCourseId());
                preCourse.setCourseName(preCourseInsert.getCourseName()[i]);
                preCourse.setUploadUid(MyHandlerInterceptor.getUid());
                preCourseMapper.insert(preCourse);
            }
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public Response getPreCourse(int courseId) {
        if(!CourseCache.courseMap.containsKey(courseId)) {
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        List<PreCourseGet> preCourseGetList;
        preCourseGetList = preCourseMapper.getPreCourse(courseId);
        return Response.ok(preCourseGetList);
    }
}




